//
//  ST_PushDailyProgressView.swift
//  btvideo
//
//  Created by mac on 2020/7/23.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit

class ST_PushDailyProgressView: UIView {
 
    static let fwWidth = ScreenWidth - 32*2
    
    static let fwHeight = 80.cgFloat
    
    private override init(frame: CGRect) {
        super.init(frame: frame)

        self.backgroundColor = UIColor.clear
        
        self.layer.shadowColor = UIColor.init(hexString: "#B6B6B6")!.cgColor
        self.layer.shadowOffset = CGSize(width: 0, height: 2)
        self.layer.shadowOpacity = 1
        self.layer.shadowRadius = 10
        self.addSubview(corner_radius_view)
        corner_radius_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(15)
            make.bottom.equalToSuperview().offset(-15)
            make.width.equalTo(cover_image_view.snp.height)
        }
        
        self.addSubview(uploading_label)
        uploading_label.snp.makeConstraints { (make) in
            make.left.equalTo(cover_image_view.snp.right).offset(10)
            make.top.equalTo(cover_image_view).offset(5)
        }
        
        
        self.addSubview(slider_root_view)
        slider_root_view.snp.makeConstraints { (make) in
            make.left.equalTo(cover_image_view.snp.right).offset(10)
            make.top.equalTo(uploading_label.snp.bottom).offset(5)
            make.right.equalToSuperview().offset(-25)
            make.height.equalTo(3)
        }
        
        slider_root_view.addSubview(slider_value_view)
        slider_value_view.snp.makeConstraints { (make) in
            make.left.top.bottom.equalTo(slider_root_view)
            make.width.equalTo(1)
        }
        
        
        
        
        
        
        self.addSubview(success_image_view)
        success_image_view.snp.makeConstraints { (make) in
            make.left.equalTo(cover_image_view.snp.right).offset(13)
            make.centerY.equalTo(cover_image_view)
            make.width.equalTo(16)
            make.height.equalTo(16)
        }
        
        self.addSubview(success_label)
        success_label.snp.makeConstraints { (make) in
            make.left.equalTo(success_image_view.snp.right).offset(5)
            make.centerY.equalTo(cover_image_view)
        }
        
        
    }
    
//    override func layoutSubviews() {
//        super.layoutSubviews()
//
//        self.roundCorners([UIRectCorner.topRight,UIRectCorner.bottomLeft,UIRectCorner.bottomRight], radius: 20)
//    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - properties
   
    fileprivate lazy var corner_radius_view:UIView = {
        let control = UIView()
        control.backgroundColor = .white
        control.cornerRadius = 15
        return control
    }()

    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 10
        return control
    }()
    
    fileprivate lazy var uploading_label:UILabel = {
        let control = UILabel.init()
        control.text = "日常上传中…"
        control.textColor = UIColor.init(hexString: "#333333")
//        control.numberOfLines = 0
//        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        return control
    }()
    
    
    fileprivate lazy var slider_root_view:UIView = {
        
        let control = UIView()
        control.backgroundColor = UIColor.init(hex: 0xF2F4F7)
        control.cornerRadius = 1.5
        return control
    }()
    
    fileprivate lazy var multi_slider_value_map = [Int:CGFloat]()
    
    fileprivate lazy var slider_value_view:UIView = {
        
        let control = UIView()
        control.backgroundColor = UIColor.init(hex: 0xFEC207)
        control.cornerRadius = 1.5
        return control
    }()
    
    fileprivate lazy var success_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.image = ST_ImageUtils.imageName("个人中心_icon_选择sel")
        control.isHidden = true
        return control
    }()
    
    fileprivate lazy var success_label:UILabel = {
            let control = UILabel.init()
            control.text = "日常发布成功"
            control.textColor = UIColor.init(hexString: "#333333")
    //        control.numberOfLines = 0
    //        control.textAlignment = .center
            control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        
        control.isHidden = true
            return control
    }()
    
//     lazy var look_btn:UIButton = {
//
//            let control = UIButton.init(type: UIButton.ButtonType.custom)
//            control.backgroundColor = Color.init(hex: 0xFEC207)
//            control.cornerRadius = 24/2.0
//    //            control.borderColor = Color.init(white: 0, alpha: 0.1)
//    //            control.borderWidth = 1
//            control.setTitleForAllStates("去看看")
//            control.setTitleColorForAllStates(.white)
//            control.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
//            control.addTarget(self, action: #selector(preview_handle), for: UIControl.Event.touchUpInside)
//
//            control.isHidden = true
//
//            return control
//    }()

    

    
//    MARK: - Progress Handle
//    支持多图模式
    func upload(progressPercent:CGFloat,currIdx:Int,count:Int)  {
        main_thread_execute {
            
            self.multi_slider_value_map.updateValue(progressPercent, forKey: currIdx)
        
            let currProgress = self.multi_slider_value_map.values.reduce(0,+)
        
            let pregress = currProgress/count.cgFloat
        
            self.upload(progressPercent: pregress)
        }
    }
    
    
    func upload(progressPercent:CGFloat)  {
        
        main_thread_execute {
            
            self.isHidden = false
        
            let width = self.slider_root_view.width * progressPercent
        
            self.slider_value_view.snp.updateConstraints { (make) in
                make.width.equalTo(width)
            }
            print("打印 特质 progressPercent-",progressPercent)
        }
    }
    
    func start_upload(coverImage:UIImage){
        
        main_thread_execute {
            
            self.multi_slider_value_map.removeAll()
                    
            self.uploading_label.isHidden = false
            
            self.slider_root_view.isHidden = false
            
            self.success_image_view.isHidden = true
            
            self.success_label.isHidden = true
            
            self.isHidden = false
            
    //        let data = try! Data.init(contentsOf: URL.init(fileURLWithPath: task.coverImagePath))
            
            self.cover_image_view.image = coverImage
            
        }
        
        
    }
    
    var upload_finished_id:String?
    
    func end_upload_status(_ status:ST_FWPushTaskStatus,videoId:String){
        
        main_thread_execute {
            
            self.upload_finished_id = videoId
            
            if status == .success{
                
                self.uploading_label.isHidden = true
                
                self.slider_root_view.isHidden = true
                
                self.success_image_view.isHidden = false
                
                self.success_label.isHidden = false
                
                main_thread_execute(afterDelay: 5) {
                    
                    self.isHidden = true
                }
            }
            if status == .failure{
                self.isHidden = true
            }
            
            self.slider_value_view.snp.updateConstraints { (make) in
                make.width.equalTo(1)
            }
            
            self.multi_slider_value_map.removeAll()
        }
        
        
    }
    
}
